/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */


var EXPORTED_SYMBOLS = ["loadDefaultPrefs"];

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cu = Components.utils;

Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/Services.jsm");


let loadedPaths = {};

function loadDefaultPrefs(addon, prefsPath)
{
    if (prefsPath in loadedPaths)
        return;
    
    if (! addon.hasResource(prefsPath))
        return;
    
    let prefsUri = addon.getResourceURI(prefsPath);
    
    let defaultPrefs = Services.prefs.getDefaultBranch("");
    let scope = {};
    scope.pref = function(key, value)
    {
        switch (typeof(value))
        {
            case "boolean":
                defaultPrefs.setBoolPref(key, value);
                break;
            case "number":
                defaultPrefs.setIntPref(key, value);
                break;
            case "string":
                var str = Cc["@mozilla.org/supports-string;1"].createInstance(Ci.nsISupportsString);
                str.data = value;
                defaultPrefs.setComplexValue(key, Ci.nsISupportsString, str);
                break;
        }
    };
    
    Services.scriptloader.loadSubScript(prefsUri.spec, scope);
    loadedPaths[prefsPath] = true;
}
